<template>
  <div>
    <!-- 表格部分 -->
    <el-table
      ref="singleTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      fit
      highlight-current-row
      :height="tableHeight"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80"
      />
      <el-table-column
        label="生成状态"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.status | statusFilter() }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="单位编码"
        prop="agencyCode"
        align="center"
      />
      <el-table-column
        label="单位名称"
        prop="agencyName"
        align="center"
      />
      <el-table-column
        label="年度"
        prop="year"
        align="center"
      />
      <el-table-column
        label="实发工资"
        align="center"
      >
      <template slot-scope="{row}">
          <span style="float: right">{{ Number(row.realSalary).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--分页部分-->
    <div class="size">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import {
  queryGenerateBankDataPage,
  generateAndExportBankData,
  revokeGenerate
} from '@/api/thirteen/generateBankData'
import Pagination from '@/components/Pagination'
import tableEmpty from '@/components/table/empty'

export default {
  name: 'IsGenerateBankDataChildren',
  components: {
    Pagination,
    tableEmpty
  },
  filters: {
    
    // 生成状态
    statusFilter(type) {
      if (type == '0') {
        return '未生成'
      } else if (type == '1') {
        return '已生成'
      }
    }
  },
  props: ['tabPane'],
  data() {
    return {
      tableHeight: null,
      tableFullScreenHeight: null,
      treeShow: false,
      listLoading: true,
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 500
      },
      agencyId: '',
      salaCardBank: ''
    }
  },
  watch: {
    list(val) {
      this.doLayout()
    }
  },
  mounted() {
    this.getTbHeight()
    this.getTbFullScreenHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
    window.addEventListener('resize', this.getTbFullScreenHeight)
  },
  created() {
    this.agencyId = ''
    this.listQuery.tabPane = this.tabPane
    this.handleFilter()
  },
  methods: {
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singleTable.doLayout()
      })
    },
    getTbHeight() {
        this.tableHeight = window.innerHeight - 228
    },
    getTbFullScreenHeight() {
      this.tableFullScreenHeight = window.innerHeight - 140
    },
    handleFilter() {
      this.listQuery.page = 1
      this.listQuery.agencyId = this.agencyId
      this.getList()
    },
    
    getList() {
      this.listLoading = true
      queryGenerateBankDataPage(this.listQuery).then(res => {
        this.list = res.data.rows
        this.total = res.data.total
        this.listLoading = false
      })
    },
    
    
    
    generateAndExportBankData() {
      generateAndExportBankData().then(res => {
        const link = document.createElement('a')
        const objectUrl = URL.createObjectURL(res)
        link.href = objectUrl
        link.download = '银行数据.xls'
        link.click()
        URL.revokeObjectURL(objectUrl)
        this.handleFilter()
        this.$emit('handleGenerateBtnLoading')
      }).catch(() => {
        this.$emit('handleGenerateBtnLoading')
      })
    },
    //撤销生成
    regenerate(){
        this.$confirm('此操作将撤销生成, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
         revokeGenerate().then(response => {
            this.$message({
              showClose: true,
              message: '撤销生成成功',
              type: 'success',
              duration: 4000
            })
            this.handleFilter()
            this.$emit('revokeGenerateSuccess')
       })
      }).catch(() =>{
        this.$emit('revokeGenerateSuccess')
      })
    },
   
    // 树的点击事件
    handleTree(data) {
      this.agencyId = data.id
      this.handleFilter()
    },
    // 切换树是否显示
    toggerTree() {
      this.treeShow = !this.treeShow
    },
    query(salaCardBank) {
      this.listQuery.salaCardBank = salaCardBank
      this.getList()
    }
  }
}
</script>

